package com.cote.douhahou.pretloan.beans;

import static com.cote.douhahou.pretloan.utils.AkomapaLendConfigs.StateFail;
import static com.cote.douhahou.pretloan.utils.AkomapaLendConfigs.StateNoORder;
import static com.cote.douhahou.pretloan.utils.AkomapaLendConfigs.StateREpay;
import static com.cote.douhahou.pretloan.utils.AkomapaLendConfigs.StateWait;

import android.text.TextUtils;

import com.cote.douhahou.pretloan.status.ApplySAkomapalendtatus;
import com.cote.douhahou.pretloan.status.OrdStaAkomapalendtus;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class ProcessInAkomapalendfo {

    private static class Inner_akomapalend {
        private static ProcessInAkomapalendfo instance = new ProcessInAkomapalendfo();
    }

    private ProcessInAkomapalendfo() {}

    public static ProcessInAkomapalendfo instance() {
        return Inner_akomapalend.instance;
    }

    public static void parse_akomapalend(JSONObject jsonObject) {
        String jsonStr = jsonObject.toString();
        Gson gson = new Gson();
        Inner_akomapalend.instance = gson.fromJson(jsonStr, ProcessInAkomapalendfo.class);
        try {
            Inner_akomapalend.instance.srcJSON = new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //判断当前订单状态显示对应的界面
        if (Inner_akomapalend.instance.isOrderPendingRepayment_akomapalend()) {//是否在待还款-未逾期
            Inner_akomapalend.instance.setLoanState_akomapalend(LoanState_akomapalend.Order_Pending_Repayment);
        } else if (Inner_akomapalend.instance.isOrderOverdueRepayment_akomapalend()) {//是否在待还款-已逾期
            Inner_akomapalend.instance.setLoanState_akomapalend(LoanState_akomapalend.Order_Overdue_Repayment);
        } else if (Inner_akomapalend.instance.isOrderLoaning_akomapalend()) {//是否在待放款
            Inner_akomapalend.instance.setLoanState_akomapalend(LoanState_akomapalend.Order_Loaning);
        } else if (Inner_akomapalend.instance.isLoanRejcted_akomapalend()) {//申请是否被拒绝
            Inner_akomapalend.instance.setLoanState_akomapalend(LoanState_akomapalend.Loan_Rejected);
        } else if (Inner_akomapalend.instance.isLoanReviewing_akomapalend()) {//是否有审核中的贷款
            Inner_akomapalend.instance.setLoanState_akomapalend(LoanState_akomapalend.Loan_Reviewing);
        } else {
            Inner_akomapalend.instance.setLoanState_akomapalend(LoanState_akomapalend.No_Order);
        }
    }

    private String product;
    private ArrayList<String> allStepTitles;
    private ArrayList<String> allSteps;
    private ArrayList<String> steps;
    private LoanState_akomapalend loanState;
    private String idCard;
    private String idCardImg;
    private String birthDate;
    private String applyStatus;//申请订单状态
    private String orderStatus;//还款订单状态
    private String deniedBetweenDay;//被拒重新申请天数
    private String userType;
    private JSONObject srcJSON;

    private String recipientName;

    public String getProduct_akomapalend() {
        if (TextUtils.isEmpty(product)) return "";
        return product;
    }

    public int getStepsSize_akomapalend(){
        if (steps==null) return 0;
        return steps.size();
    }

    public String getIdCard_akomapalend() {
        if (TextUtils.isEmpty(idCard)) return "";
        return idCard;
    }

    public String getIdCardImg_akomapalend() {
        if (TextUtils.isEmpty(idCardImg)) return "";
        return idCardImg;
    }

    public String getStepName_akomapalend(int index){
        if (steps==null||index<0||index>=steps.size()) return "";
        return steps.get(index);
    }

    public String getH5Url_akomapalend(){
        if (srcJSON==null) return "";
        return srcJSON.optString("h5");
    }

    public ProceAkomapalendssInfoStep getStep_akomapalend(int index){
        try {
            if (steps==null||index<0||index>=steps.size()||srcJSON==null) return null;
            String stepName = steps.get(index);

            String stepStr = "";

            if (srcJSON.has(stepName+"_desc")) stepStr = srcJSON.optString(stepName+"_desc");
            else stepStr = srcJSON.optString(stepName);


            if (TextUtils.isEmpty(stepStr) || stepStr.equalsIgnoreCase("null") || !stepStr.startsWith("{"))
                stepStr = AppAkomapalendConfigCustomize.instance().getOther_akomapalend(stepName+"_desc");

            if (TextUtils.isEmpty(stepStr)) return null;

            return new Gson().fromJson(stepStr, ProceAkomapalendssInfoStep.class);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    public ArrayList<String> getSteps_akomapalend() {
        return steps;
    }

    public int getPercent_akomapalend(){
        if (steps==null||allSteps==null||steps.isEmpty()||allSteps.isEmpty()) return 0;
        int finishStepSize = allSteps.size()-steps.size();
        if (finishStepSize<0) return 100;
        int result = (int) Math.round(((double)finishStepSize)/((double)allSteps.size())*100);
        if (result == 100) result--;
        return result;
    }

    public void setLoanState_akomapalend(LoanState_akomapalend loanState) {
        this.loanState = loanState;
    }

    public String getOrderStatus_akomapalend() {
        if (TextUtils.isEmpty(orderStatus)) return "";
        return orderStatus;
    }

    public String getApplyStatus_akomapalend() {
        if (TextUtils.isEmpty(applyStatus)) return "";
        return applyStatus;
    }

    public String getDeniedBetweenDay_akomapalend() {
        if (TextUtils.isEmpty(deniedBetweenDay)) return "30";
        return deniedBetweenDay;
    }

    public ArrayList<String> getAllSteps_akomapalend() {
        if (allSteps==null) return new ArrayList<>();
        return allSteps;
    }

    public String getRecipientName_akomapalend() {
        if (TextUtils.isEmpty(recipientName)) return "";
        return recipientName;
    }

    public boolean isNewUser_akomapalend(){
        if (TextUtils.isEmpty(recipientName)) return true;
        return !userType.equalsIgnoreCase("OLD");
    }

    public String getStepTitle_akomapalend(int index){
        if (allStepTitles==null || index<0 || index>=allStepTitles.size()) return "";
        return allStepTitles.get(index);
    }

    public String getProcessNum_akomapalend(){
        if (allSteps==null||steps==null||allSteps.isEmpty()) return "0";
        int result=0;
        result = allSteps.size()-steps.size()+1;
        return String.valueOf(Math.max(result, 0));
    }

    /**
     * 待放款
     */
    public boolean isOrderLoaning_akomapalend() {
        return (getOrderStatus_akomapalend().equalsIgnoreCase(OrdStaAkomapalendtus.CREATED_akomapalend) ||
                getOrderStatus_akomapalend().equalsIgnoreCase(OrdStaAkomapalendtus.LEN_akomapalendDING));
    }

    /**
     * 待还款-未逾期
     */
    public boolean isOrderPendingRepayment_akomapalend() {
        return getOrderStatus_akomapalend().equalsIgnoreCase(OrdStaAkomapalendtus.LOA_akomapalendNED);
    }

    /**
     * 待还款-已逾期
     */
    public boolean isOrderOverdueRepayment_akomapalend() {
        return (getOrderStatus_akomapalend().equalsIgnoreCase(OrdStaAkomapalendtus.OVERDUE_akomapalend) ||
                getOrderStatus_akomapalend().equalsIgnoreCase(OrdStaAkomapalendtus.BAD_DEBT_akomapalend));
    }

    /**
     * 申请拒绝
     */
    public boolean isLoanRejcted_akomapalend() {
        return (getOrderStatus_akomapalend().equalsIgnoreCase(OrdStaAkomapalendtus.DENIED_akomapalend) ||
                getApplyStatus_akomapalend().equalsIgnoreCase(ApplySAkomapalendtatus.SYS_DE_akomapalendNIED) ||
                getApplyStatus_akomapalend().equalsIgnoreCase(ApplySAkomapalendtatus.MANU_DEN_akomapalendIED) ||
                getApplyStatus_akomapalend().equalsIgnoreCase(ApplySAkomapalendtatus.DS_DENIED_akomapalend));
    }

    /**
     * 申请审核中
     */
    public boolean isLoanReviewing_akomapalend() {
        return (getApplyStatus_akomapalend().equalsIgnoreCase(ApplySAkomapalendtatus.CREAT_akomapalendED) ||
                getApplyStatus_akomapalend().equalsIgnoreCase(ApplySAkomapalendtatus.SYS_akomapalendCHECK) ||
                getApplyStatus_akomapalend().equalsIgnoreCase(ApplySAkomapalendtatus.SYS_APROVAL_akomapalend) ||
                getApplyStatus_akomapalend().equalsIgnoreCase(ApplySAkomapalendtatus.MANU_AP_akomapalendROVAL) ||
                getApplyStatus_akomapalend().equalsIgnoreCase(ApplySAkomapalendtatus.DS_APROVAL_akomapalend));
    }

    public String getAkomapaLendUtil(){
        if (isOrderPendingRepayment_akomapalend() || isOrderOverdueRepayment_akomapalend()) //订单状态-待还款
            return StateREpay;
        else if (isOrderLoaning_akomapalend() || isLoanReviewing_akomapalend()) //订单状态-放款中/审核中
            return StateWait;
        else if (isLoanRejcted_akomapalend()) //订单状态-被拒绝
            return StateFail;
        else return StateNoORder;
    }

    public enum LoanState_akomapalend {
        ///////////////////
        No_Order,//No 订单
        Loan_Rejected,//申请拒绝
        Loan_Reviewing,//申请审核中
        Order_Loaning,//待放款
        Order_Pending_Repayment,//待还款，未逾期
        Order_Overdue_Repayment,//待还款,已逾期
    }

}
